/*
 * include.cpp
 *
 *  Created on: 03.09.2012
 *      Author: caffeine
 */


#include "include.h"
#include "../CXMLFactoryEnvironment.h"
#include <lampe/xml/IFactoryRecord.h>
#include <lampe/xml/IXmlNode.h>


namespace lampe{
namespace xml{

	#define MARKER printf("%s(%d)\n",__FILE__,__LINE__)

class IncludeFactoryRecord:public lampe::xml::IFactoryRecord{
public:
	virtual lampe::xml::TagResult tagHandlerBefor(lampe::xml::IXMLFactoryEnvironment*environment,lampe::xml::IXmlNode*node){
		printf("try include file \"%s\"\n",node->getAttString("href","").c_str());
		lampe::xml::IXmlNode*root=environment->createXmlTree(node->getAttString("href",""));
		if (!root)
			return lampe::xml::STOP;
		root->setParent(node);
		root->drop();
		irr::s32 index=node->getParent()->indexOf(node);
		if (index>=0){
			node->dropChildToEx(node->getParent(),index);
		}else{
			node->dropChildTo(node->getParent());
		}
		node->setParent(0);
		return lampe::xml::BACK;
	}
	virtual lampe::xml::TagResult tagHandlerAfter(lampe::xml::IXMLFactoryEnvironment*environment,lampe::xml::IXmlNode*node){
		return lampe::xml::BACK;
	}
	virtual LAMPE_VECTOR_VENDOR<String> getProcessedTags(){
		LAMPE_VECTOR_VENDOR<String> res;
		res.push_back("include");
		return res;
	};
};

}//namespace xml
}//namespace lampe

void regInclude(lampe::xml::CXMLFactoryEnvironment*environment){
	lampe::xml::IncludeFactoryRecord*temp=new lampe::xml::IncludeFactoryRecord();
	environment->registerHandler(temp);
	temp->drop();
}
